package com.sprd.simple.launcher;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sprd.simple.adapter.BaseListViewAdapter;
import com.sprd.simple.model.TextSpeech;
import com.sprd.simple.util.StatusBarUtils;
//import com.sprd.android.support.featurebar.FeatureBarHelper;

import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.content.Intent;

import com.sprd.simple.util.KeyCodeEventUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SPRD on 2016/11/11.
 */

public abstract class BaseActivity<T> extends Activity implements AdapterView.OnItemLongClickListener,
        AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {

    protected Context mContext;
    protected String[] appNameForTTS;
    protected BaseListViewAdapter mAdapter = null;
    private ListView mListView;
    protected List<T> mInfos;
    private static final String TAG = "BaseActivity";
    public static final String DATABASE = "FamilyNumber";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        StatusBarUtils.setWindowStatusBarColor(this); // change status bar color
        mContext = this;
        mInfos = loadData();
        if (mInfos == null) {
            mInfos = new ArrayList<>();
        }
    }

    protected void initListView(ListView listView, BaseListViewAdapter adapter) {
        mListView = listView;
        mAdapter = adapter;
        mAdapter.setPosition(0);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemLongClickListener(this);
        mListView.setOnItemClickListener(this);
        mListView.setOnItemSelectedListener(this);
    }

    /**
     * Each item click and enter different app
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        launchApp(position);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mAdapter.setPosition(position);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    /**
     * Long press any item in this page, it will send a broadcast to SOS
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     * @return
     */
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
                                   int position, long id) {
        Intent intent = new Intent(Launcher.SOS_BROADCAST);
        sendBroadcast(intent);
        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (event.isTracking() && !event.isCanceled()) {
            Log.i(TAG, "onKeyUp  short press");
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                return super.onKeyUp(keyCode, event);
            }
            KeyCodeEventUtil.pressKeyEventForMainActivity(mContext, keyCode, event);
        } else {
            Log.i(TAG, "onKeyUp  long press");
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getRepeatCount() == 0) {
            Log.i(TAG, "getRepeatCount");
            event.startTracking();
            return true;
        }
        return false;
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        KeyCodeEventUtil.longPressKeyEventForMainActivity(mContext, keyCode, event);
        return true;
    }

    abstract void launchApp(int position);

    abstract List<T> loadData();

}
